/**
 * @file skoll.actions.info.edittagsof.js
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: skoll.actions.info.edittagsof.js 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/skins/Skoll/js/skoll.actions.info.edittagsof.js $
 */

Skoll.actions.info.edittagsof = {
	//
	// Public methods.
	removeTagFrom: function(tag, itemType, item) {
//		Skoll.tools.Loading.start(0);
//
		var service = "tags:remove:";
		var what = "";
		switch (itemType) {
			case "CX":
				what = "comic";
				break;
			case "IM":
				what = "image";
				break;
		}
		var serviceUrl = "service.php?service=" + service + what + "&group=" + GROUP_ID + "&" + what + "=" + item + Skoll.tools.extraUrlParams();

		$.ajax({
			dataType: "json",
			url: serviceUrl,
			type: "POST",
			data: {
				params: JSON.stringify({
					list: [tag]
				})
			},
			success: function(data) {
				if (data.status) {
					Skoll.tools.log("Tags settings success");
					$("#EditTagsOf_TagsBox").wcGetDelayedReload();
//					Skoll.tools.Loading.stop();
				} else {
					Skoll.tools.log("Tags settings error: [" + data.error.code + "] " + data.error.message);
//					Skoll.tools.Loading.stop();
				}
			},
			error: function(data) {
				Skoll.tools.log("Tags settings error: " + data);
//				Skoll.tools.Loading.stop();
			}
		});
	},
	sendTags: function(form, event) {
		Skoll.tools.preventDefault(event);

		var what = $(form).find("#edituriname").val();
		var id = $(form).find("#editid").val();
		var service = "tags:add:" + what;
		var tags = $(form).find("#editnewtags").val().split(",");

		if ($(form).find("#editmode").val() == "REMOVE") {
			service = "tags:remove:comic";
		}

		var serviceUrl = "service.php?service=" + service + "&group=" + GROUP_ID + "&" + what + "=" + id + Skoll.tools.extraUrlParams();
		Skoll.tools.log("Skoll.actions.info.edittagsof.sendTags(): URL: " + serviceUrl);

		$(form).find("#editnewtags").val("");
		$(form).find("#EditTagsOf_TagsBox").append("loading...");

		$(form).find("#editnewtags").val("");
		$.ajax({
			dataType: "json",
			url: serviceUrl,
			type: "POST",
			data: {
				params: JSON.stringify({
					list: tags
				})
			},
			success: function(data) {
				if (data.status) {
					Skoll.tools.log("Tags settings success");
					$(form).find("#EditTagsOf_TagsBox").wcGetDelayedReload();
				} else {
					Skoll.tools.log("Tags settings error: [" + data.error.code + "] " + data.error.message);
				}
			},
			error: function(data) {
				Skoll.tools.log("Tags settings error: " + data);
			}
		});
	}
}
$(document).ready(function() {
	$(document).on("click", ".SkollMethod-RemoveTag", function(event) {
		Skoll.tools.preventDefault(event);
		var info = $.parseJSON($(this).attr("skollinfo"));
		Skoll.actions.info.edittagsof.removeTagFrom(info.tag, info.itemType, info.item);
		$(this).remove();
	});
});